package com.lwl.Algorithmic_data_structure.动态规划.class5;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class queen {
    List<List<String>> res = new ArrayList<>();
    public List<List<String>> solveNQueens(int n) {
        if(n < 1){
            return null;
        }
        int[] record = new int[n];
        char[][] board = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] = '.';
            }
        }
        process(0,record,n,board);
        return res;
    }

    private void process(int i, int[] record, int n, char[][] board) {
        if(i == n){
            List<String> list = new ArrayList<>();
            for (int k = 0; k < n; k++) {
                list.add(String.valueOf(board[k]));
            }
            res.add(list);
            return ;
        }
        for (int j = 0; j < n; j++) {
            if(isValid(record,i,j)){
                record[i] = j;
                board[i][j] = 'Q';
                process(i+1,record,n, board);
                board[i][j] = '.';
            }
        }
    }

    private boolean isValid(int[] record, int i, int j) {
        for (int k = 0; k < i; k++) {
            if(record[k] == j || Math.abs(i-k) == Math.abs(j-record[k])){
                return false;
            }
        }
        return true;
    }
    @Test
    public void test(){
        Map<String,Integer> map = new HashMap<>();
        String s1 = new String("123");
        String s2 = new String("123");
        map.put(s1,1);
        map.put(s2,2);
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
            System.out.println(stringIntegerEntry.hashCode() + "|" + stringIntegerEntry.getValue());
        }
    }
}
